<?php

namespace App\Http\Controllers\Api\Admin;

use Illuminate\Http\Request;
use App\Http\Requests\Api\Admin\AdminPowerRequest;
use App\Http\Requests\Api\Admin\AdminPowerUpdateRequest;
use App\Http\Requests\Api\Admin\AdminPowerSetRoleRequest;
use App\Http\Requests\Api\Admin\AdminPowerSetRouteRequest;
use App\Http\Resources\Api\Admin\AdminPowerResource;
use App\Http\Resources\Api\Admin\AdminPowerRoleResource;
use App\Http\Resources\Api\Admin\AdminPowerRoleHasNameResource;
use App\Models\Admin\AdminPower;
use App\Models\Admin\AdminPowerRoute;
use App\Models\Admin\AdminPowerRole;
use App\Models\Admin\AdminRole;
use App\Http\Queries\AdminPowerQuery;
use App\Http\Business\PowerBusiness;
use Illuminate\Database\Eloquent\Builder;
use App\Http\Controllers\Api\Controller;

class PowerManageController extends Controller
{
    /**
     * 得到所有的权限
     */
    public function powers()
    {
        $powers = AdminPower::with('routes')->get();
        $res = AdminPowerResource::collection($powers);
        return $this->success($res);
    }


    //返回路由列表
    public function index(AdminPowerQuery $query){
        //3个用户为一页
        $routes = $query->paginate($this->getPageSize());

        return AdminPowerResource::collection($routes);
    }

    //刷新路由权限
    public function refresh()
    {
        $res = (new PowerBusiness())->addNotAddPower();

        return $this->success('刷新成功');

    }

    /**
     * 添加权限
     * @param AdminPowerRequest $request
     * @return mixed
     */
    public function add(AdminPowerRequest $request)
    {
        $attributes = $request->only(['power' ,'power_name','controller', 'controller_name','module' ,'module_name', 'content','action', 'is_check']);
        $res = AdminPower::create($attributes);

        return $this->success(new AdminPowerResource($res));
    }

    //更新路由
    public function update(AdminPowerUpdateRequest $request)
    {
        $power = AdminPower::find($request->input('power_id'));
        $attributes = $request->only(['power_name', 'controller_name', 'module_name', 'content', 'is_check']);
        if ($power){
            $power->update($attributes);
            return $this->success(new AdminPowerResource($power));
        }

        return $this->failed('更新失败');
    }

    public function delete(Request $request)
    {
        $power_id = $request->input('power_id');
        if (!$power_id){
            return $this->failed('删除权限失败!');
        }

        $power = AdminPower::find($power_id);
        if ($power){
            $power->delete();
            AdminPowerRole::where(['power_id' => $power_id])->delete();
            return $this->success('删除权限成功');
        }

        return $this->failed('删除路由失败！');
    }

    public function getPower(Request $request)
    {
        $route_id = $request->input('route_id');
        if ($route_id){
            $power = AdminPower::whereHas('routes', function (Builder $query) use ($route_id) {
                $query->where('route_id', '=', $route_id);
            })->with('roles')->get();

            return $this->success(AdminPowerResource::collection($power));
        }

        return $this->failed('获取失败');
    }

    public function setRole(AdminPowerSetRoleRequest $request)
    {
        $power_id = $request->input('power_id');
        $role_id = $request->input('role_id');
        $status = $request->input('status');
        $role = AdminRole::find($role_id);
        $power = AdminPower::find($power_id);

        if ($role && $power){
            $data = ['role_id' => $role_id, 'power_id' => $power_id];
            $powerRole = AdminPowerRole::where($data)->first();
            if ($powerRole){
                $status < 1 && $powerRole->delete();
            }else{
                $status > 0 && AdminPowerRole::create($data);
            }
            return $this->success('设置成功');
        }else{
            return $this->failed('设置失败');
        }
    }

    /**
     * 设置权限和路由的关联
     * @param AdminPowerSetRouteRequest $request
     * @return mixed
     */
    public function setRoute(AdminPowerSetRouteRequest $request)
    {
        $power_id = $request->input('power_id');
        $route_id = $request->input('route_id');
        $status = $request->input('status');
        $data = ['route_id' => $route_id, 'power_id' => $power_id];
        $powerRoute = AdminPowerRoute::where($data)->first();
        if ($powerRoute){
            $status < 1 && $powerRoute->delete();
        }else{
            $status > 0 && AdminPowerRoute::create($data);
        }
        return $this->success('设置成功');
    }

}
